/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = frcmd_parse.c                                      */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/03 by guojq                                */
/*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#include "frcomm.h"
#include "frmem.h"
#include "frcmd_parse.h"
#include "frcmd_client.h"
#include "frcmd_parse_detail.h"
#include "client.pb-c.h"
#include "frsutl.h"

/* ip|port
 *  192.168.0.1|80
 *  |80
 *  192.168.0.1-192.168.0.50|80-1000
 */
char* contact_ipport(char *ip, char *port)
{
	char *local_str = NULL;
	if(ip!=NULL && port!=NULL) {
		local_str = malloc(strlen(ip) + strlen(port) + 2); 
		sprintf(local_str, "%s|%s", ip, port);
		free(ip);
		free(port);
	} else if(ip!=NULL) {
		local_str = ip;
	} else { /* port is not null */
		local_str = malloc(strlen(port) + 2); 
		sprintf(local_str, "|%s", port);
		free(port);
	}
	return local_str;
}

/* l7 操作 */
int l7_add(char *ip_proto, char *ip_addr, char *port, char *l7_proto)
{
    int i_ret = 0;
    i_ret = frcmd_l7_cmd("add", ip_proto, ip_addr, port,l7_proto);

    if(ip_proto)
        free(ip_proto);
    if(ip_addr)
        free(ip_addr);
    if(port)
       free(port);
    return i_ret;
}

int l7_del(char *ip_proto, char *ip_addr, char *port)
{
    int i_ret = 0;
    i_ret = frcmd_l7_cmd("del", ip_proto, ip_addr, port,NULL);

    if(ip_proto)
        free(ip_proto);
    if(ip_addr)
        free(ip_addr);
    if(port)
       free(port);
    return i_ret;
}

int l7_default_add(char *ip_proto, char *l7_proto)
{
    int i_ret = 0;
    i_ret = frcmd_l7_default("add",ip_proto, l7_proto);

    if(ip_proto)
        free(ip_proto);
    if(l7_proto)
        free(l7_proto);
    return i_ret;
}


int l7_default_del(char *ip_proto)
{
    int i_ret = 0;
    i_ret = frcmd_l7_default("del",ip_proto,NULL);

    if(ip_proto)
        free(ip_proto);

    return i_ret;
}

int l7_default_clear(void)
{
    int i_ret = 0;
    i_ret = frcmd_l7_default("clear",NULL,NULL);

    return i_ret;
}

int l7_clear(void)
{
    return 0;
}

int filter_add(char *macaddr, char* ip_proto, char *src_iprange_portrange, char *dst_iprange_portrange)
{
	frcmd_filter("add", ip_proto, src_iprange_portrange, dst_iprange_portrange, macaddr);
	
	if (ip_proto)
	    free(ip_proto);
	if (src_iprange_portrange)
	    free(src_iprange_portrange);
	if (dst_iprange_portrange)
	    free(dst_iprange_portrange);
	if (macaddr)
	    free(macaddr);
	
	return 0;
}

int filter_del(char *macaddr, char* ip_proto, char *src_iprange_portrange, char *dst_iprange_portrange)
{
	frcmd_filter("del", ip_proto, src_iprange_portrange, dst_iprange_portrange, macaddr);
	
	if (ip_proto)
	    free(ip_proto);
	if (src_iprange_portrange)
	    free(src_iprange_portrange);
	if (dst_iprange_portrange)
	    free(dst_iprange_portrange);
	if (macaddr)
	    free(macaddr);
	    
	return 0;	
}

int filter_clear(void)
{
	frcmd_filter_clear();
	
	return 0;
}


/* ip_addr==NULL 表示 localhost */
int java_setup(char *ip_proto, char *ip_addr, int port)
{
	printf("java_setup %s %s %d\n", ip_proto, ip_addr, port);
	return 0; 
}


int commit(int obj_type)
{
	frcmd_commit(CMD_ACTION_COMMIT, obj_type);
	
	return 0;
}
int rollback(int obj_type)
{
	frcmd_commit(CMD_ACTION_ROLLBACK, obj_type);
	
	return 0;
}

/* export */
int export_pcap(char *ip_proto, char *ipaddr, int port)
{
	return 0;
}

/* print */
int print_system(void)
{
	frcmd_client_print("system");
	return 0;
}

int print_memory(void)
{
	frcmd_client_print("memory");
	return 0;
}

int print_ct(char *l7_proto)
{
	frcmd_print_ct(l7_proto);

    if (l7_proto)
	    free(l7_proto);

	return 0;
}

int print_topology(void)
{
	frcmd_client_print("topology");
	return 0;
}

int print_thread(void)
{
	frcmd_client_print("thread");
	return 0;
}

int print_module(char *p_name)
{
    if(strcasecmp(p_name,"config") == 0)
	    printf("print module config\n");
	else if(strcasecmp(p_name,"stat") == 0)
	    printf("print module stat\n");
	else
	    return 1;
    frcmd_print_module(p_name);
	return 0;
}

int print_server(void)
{
	frcmd_client_print("server");
	return 0;
}

int print_filter(void)
{
	frcmd_client_print("filter");
	return 0;
}

int print_java(void)
{
	frcmd_client_print("java");
	return 0;
}
int print_l7(void)
{
	frcmd_client_print("l7_default");
	return 0;
}

void print_help(void)
{
	frcmd_usage();
}

void server_exit(void)
{
    frcmd_exit();

}

int module_add(char *p_path, char *l4_proto, int l7_proto, char *p_l7_proto_name,int mem_size)
{
    int i_ret = 0;
    i_ret = do_module_cmd("add", p_path, l4_proto, l7_proto, p_l7_proto_name, mem_size);

    if(p_path)
        free(p_path);
    if(p_l7_proto_name)
        free(p_l7_proto_name);
    if(l4_proto)
        free(l4_proto);

    return i_ret;
}

int module_del(int l7_proto)
{
    int i_ret = 0;
    i_ret = do_module_cmd("del", NULL, NULL,  l7_proto, NULL, 0);

    return i_ret;
}

int module_clear(void)
{
    int i_ret = 0;
    i_ret = do_module_cmd("clear",NULL, NULL, 0, NULL, 0);

    return i_ret;
}
